package com.example.ruoyiakp.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ruoyiakp.product.domain.AgrAgriculturalDetectQrCode;
import com.example.ruoyiakp.product.service.AgrAgriculturalDetectQrCodeService;
import com.example.ruoyiakp.product.service.AgrAgriculturalDetectService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.QRCodeUtil;
import com.ruoyi.common.core.utils.file.FileMultipartFileUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName QrCodeController
 * @Description 溯源码API
 * @Author 陈萝瑶
 * @Date 2025/3/6 15:23
 */
@CrossOrigin(origins = "*") // 允许来自这个源的请求
@RestController
@RequestMapping("/code")
public class QrCodeController extends BaseController {

    @Resource
    private RemoteFileService remoteFileService;

    @Autowired
    private AgrAgriculturalDetectService agrAgriculturalDetectService;

    @Autowired
    private AgrAgriculturalDetectQrCodeService agrAgriculturalDetectQrCodeService;


    private static final ThreadLocal<SimpleDateFormat> LOCALDATEFORMAT=ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));

    @PostMapping("/generate")
    public AjaxResult generateV1(@RequestBody String content) {
        try {
            final String fileName = LOCALDATEFORMAT.get().format(new Date());
            // 生成二维码并保存到本地临时文件
            File tempFile = new File(System.getProperty("java.io.tmpdir"), fileName + ".png");
            QRCodeUtil.createCodeToFile(content, tempFile.getParentFile(), tempFile.getName());

            MultipartFile multipartFile = new FileMultipartFileUtils(tempFile);
            R<SysFile> fileResult = remoteFileService.upload(multipartFile);
            String url = fileResult.getData().getUrl();
            agrAgriculturalDetectService.updateUrl(url);
            // 清理临时文件
            if (!tempFile.delete()) {
                System.err.println("Failed to delete temporary file: " + tempFile.getAbsolutePath());
            }
            return AjaxResult.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("生成二维码失败");
        }
    }

    @RequiresPermissions("product:code:getQrCode")
    @GetMapping("/getQrCode")
    public AjaxResult getQrCode(Long batchId){
        QueryWrapper<AgrAgriculturalDetectQrCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_id",batchId);
        return AjaxResult.success(agrAgriculturalDetectQrCodeService.getOne(queryWrapper));
    }

    @PostMapping("/insertQrCode")
    public AjaxResult insertQrCode(@RequestBody AgrAgriculturalDetectQrCode agrAgriculturalDetectQrCode){
        return AjaxResult.success(agrAgriculturalDetectQrCodeService.save(agrAgriculturalDetectQrCode));
    }

}
